const fs = require("fs")
const esprima = require("esprima")
const estraverse = require("estraverse")
const escodegen = require("escodegen")
const iconv = require("iconv-lite")
const es_tool = require("../../tools/es_ast_tool")
// const akamai_base = require("../common/akamai_base")
const babel_tool = require('../../tools/babel_ast_tool');
const base_tool =  require('../../tools/base_ast_tools');


const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p><script type="text/javascript" src="/Opence-Heere-Interd-not-sherson-And-mewd-be-Whol" async="" defer=""></script>`);
window = dom.window;
document = window.document;

// const {findFuncForConstReturn, findFuncForConstReturnStep2} = require("../common/akamai_base");


// step 1
// var ast = es_tool.get_ast_by_file("./chameleon.js");
// var repeat_vars = es_tool.find_repeat_vars(ast);
// var names = es_tool.get_new_name(ast);
// ast = base_tool.renameAllFuncParams(ast, names, repeat_vars);
// // ast = base_tool.renameAllFuncName(ast, names, repeat_vars);
// ast = base_tool.renameAllVar(ast, names, repeat_vars);
// ast = base_tool.renameAllCacheParam(ast, names, repeat_vars);

// step 2
var ast = es_tool.get_ast_by_file("./1.js");

function find_two_array(){
    var result = [];
    estraverse.replace(ast,{
        enter(node, parent){
            if(node.type==="CallExpression"&&node.arguments&&node.arguments.length===2){
                var node1 = node.arguments[0];
                var node2 = node.arguments[1];
                if(node1.type==="ArrayExpression"&&node2.type==="ArrayExpression"&&node1.elements&&node1.elements.length>200){
                    // var code1 = escodegen.generate(node1);
                    // var code2 = escodegen.generate(node2);
                    result.push(node1.elements);
                    result.push(node2.elements);
                }
            }
        }
    });
    return result
}
var arrs = find_two_array();

// 替换数组的值
function replace_arr(arr1_vars, arr2_vars){
    estraverse.replace(ast, {
        enter(node, parent){
            if(node.type==="MemberExpression"&&node.object&&node.property){
                if(arr1_vars.indexOf(node.object.name)!==-1){
                    return arrs[0][node.property.value];
                }
                if(arr2_vars.indexOf(node.object.name)!==-1){
                    return arrs[1][node.property.value];
                }
            }
        }
    })
}
replace_arr(["AGi","ABQ","ABE"],["ABO","ABR","ADM"]);

// Wn函数执行并替换
function Wn() {
    return arguments[1].split('').reverse().join('');
}
function replaceWn(){
    estraverse.replace(ast,{
        enter(node, parent){
            if(node.type==="CallExpression"&&node.callee&&node.callee.type==="Identifier"&&node.callee.name==="Wn"){
                var code = `Wn(0,"${node.arguments[1].value}")`
                // var val = eval(escodegen.generate(node));
                var val = eval(code);
                return {
                    type:"Literal",
                    value:val,
                    raw:val.toString()
                }
            }
        }
    })
}
replaceWn();

es_tool.save_ast_to_file(ast, "./2.js");
